package top.idetect.client.schedule;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import top.idetect.client.modules.cloth.model.Flaw;
import top.idetect.client.modules.cloth.service.FlawService;

import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.List;

@Slf4j
@Component
public class ImgDataResizeTask {

    @Autowired
    private FlawService flawService;

    @Value("${spring.profiles.active}")
    private String active;

//    public static boolean imgResizeFlag = true;

    @Scheduled(fixedRate = 1000 * 60)
    public void imgDataResize(){
//        log.info("active:{}", active);
        if(active.equals("prod")) {
            long beginTime = System.currentTimeMillis();
            log.info("图片数据resize");
            List<Flaw> flaws = flawService.listImgNoNail();
            log.info("需处理的图片数量:{}", flaws.size());
            for(Flaw flaw : flaws){
                if(flaw.getImgData() == null){
                    continue;
                }
                String imgData = flaw.getImgData();
                if(!imgData.startsWith("/9j/")){
                    flaw.setImgData(null);
                    flaw.setImgDataThumbnail("error");
                    continue;
                }

                // 将 base64 编码的图片数据解码为字节数组
                byte[] imageData = Base64.getDecoder().decode(imgData);

                // 加载图片
                BufferedImage inputImage;
                try {
                    inputImage = ImageIO.read(new ByteArrayInputStream(imageData));
                    // 获取图片宽度和高度
                } catch (IOException e) {
                    log.info("Error: " + e.getMessage());
                    return;
                }

                // 缩放后的宽度和高度
                int scaledWidth = 40;
                int scaledHeight = 40;

                // 创建一个 BufferedImage 对象来存储缩放后的图片
                BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, inputImage.getType());

                // 使用 Graphics2D 对象绘制缩放后的图片
                Graphics2D g2d = outputImage.createGraphics();
                g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
                g2d.dispose();

                // 将缩放后的图片转换为 base64 编码的字符串
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                try {
                    ImageIO.write(outputImage, "png", outputStream);
                } catch (IOException e) {
                    log.info("Error: " + e.getMessage());
                    return;
                }
                String resizedBase64ImageData = Base64.getEncoder().encodeToString(outputStream.toByteArray());
                flaw.setImgDataThumbnail(resizedBase64ImageData);
                flaw.setImgData(null);
//                flaw.setClassStr("异常"); //临时要求
            }

            long endTime = System.currentTimeMillis();
            log.info("resize图片压缩耗时: begin:{} -- end:{} -- 时长:{}",beginTime, endTime, endTime - beginTime);

            // 批量更新
            flawService.updateBatchById(flaws);
            long endTime1 = System.currentTimeMillis();
            log.info("resize任务耗时: begin:{} -- end:{} -- 时长:{}",beginTime, endTime1, endTime1 - beginTime);
        }

    }
}
